package com.idenger.common.utils;

import com.idenger.modules.edu.entity.user.UserToken;
import com.idenger.modules.edu.service.user.UserTokenService;


/**
 * Created by zp on 17/5/31.
 */
public class TokenUtil {
	/*@Autowired
	private static UserTokenService userTokenService;*/
    /**
     * 生成token号码
     * @return token号码
     */
    public static String generateToken() {
        return RandomUtils.randomCustomUUID().concat(RandomUtils.randomString(6));
    }

    public static int checkTokenId(String tokenId) {
        int result = 0;
       // String TokenInValid = PropKit.get("tokenInValid");
        String TokenInValid="604800";
     //   String sql = UserToken.dao.getSql("userToken.getTokenByTokenId");
     //   UserToken token = UserToken.dao.findFirst(sql,tokenId);
        UserTokenService userTokenService = SpringContextHolder.getBean("userTokenService");
        UserToken token =userTokenService.getTokenByTokenId(tokenId);
        if (token == null) {
            result = 0; // 无此验证码或验证码错误
            return result;
        }
//        test
        System.out.println(token.toString());
        if (System.currentTimeMillis() - token.getModifyTime().getTime() >= Integer.parseInt(TokenInValid) * 1000) {
            result = 2;// 验证码超时
        } else {
            result = 1;// 正确
        }
//      test
      System.out.println("================" + result);
        return result;
    }
}
